﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using OBUCare.Core.DataAccess;
using OBUCare.Core.Models;
using OBUCare.Core.Common;

namespace OBUCare.Web.Areas.Admin.Controllers
{
    public class DictTypeController : Controller
    {
        private OBU_CAREContext db = new OBU_CAREContext();

        //
        // GET: /Admin/DictType/

        public ActionResult Index()
        {
            return RedirectToAction("List");
        }
        public ActionResult ParentPage()
        {
            return View();
        }
        public ViewResult List()
        {
            var allItems = db.DICT_TYPE.ToList();
            var listItems = from i in allItems
                            orderby i.DICT_INDEX
                            select i;
            return View(listItems.ToList());
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Insert()
        {

            //Create a new instance of the EditableProduct class.
            var m = new DICT_TYPE();


            //Perform model binding (fill the product properties and validate it).
            if (ModelState.IsValid && TryUpdateModel(m))
            {
                db.DICT_TYPE.Add(m);
                db.SaveChanges();
            }
            return RedirectToAction("Index");
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Update(int id)
        {
            //Find a model whose id is equal to the id action parameter
            var m = db.DICT_TYPE.Find(id);

            if (m == null)
            {
                //A customer with the specified id does not exist - redisplay the grid

                //GridRouteValues() is an extension method which returns the
                //route values defining the grid state - current page, sort expression, filter etc.
                return RedirectToAction("Index");
            }

            //Perform model binding (fill the customer properties and validate it).
            if (TryUpdateModel(m))
            {
                //The model is valid - update the customer and redisplay the grid.
                db.SaveChanges();

                //GridRouteValues() is an extension method which returns the 
                //route values defining the grid state - current page, sort expression, filter etc.

            }

            return RedirectToAction("Index");
        }

        //
        // POST: /Admin/CodeTable/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            var m = db.DICT_TYPE.Find(id);
            //m.DICT_TYPE_VALUE.Each(i => db.DICT_TYPE_VALUE.Remove(i));
            db.DICT_TYPE.Remove(m);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
    }
}
